package com.itzxx.library_base_ktx.adapter

import android.graphics.drawable.Drawable
import android.view.View
import android.widget.CheckBox
import android.widget.ImageView
import androidx.annotation.DimenRes
import androidx.databinding.BindingAdapter
import com.bumptech.glide.Glide
import com.bumptech.glide.load.resource.bitmap.CenterCrop
import com.bumptech.glide.request.RequestOptions
import com.itzxx.library_base_ktx.R
import com.itzxx.library_base_ktx.ext.view.clickNoRepeat
import com.itzxx.library_base_ktx.utils.DisplayUtils
import com.itzxx.library_base_ktx.utils.TopCornersRoundedTransformation

/**
 * Desc:
 *
 * Author: zhangxiaoxiao
 * Date: 2024/7/2 09:34
 * Copyright: Copyright (c) 2006-2024
 * Company: @海晟融创
 * Updater:
 * Update Time:
 * Update Comments:
 */
object BindingAdapters {

    @BindingAdapter("clickNoRepeat")
    @JvmStatic
    fun setClickNoRepeat(view: View, listener: (() -> Unit)) {
        view.clickNoRepeat {
            listener.invoke()
        }
    }

    @BindingAdapter(value = ["loadDrawable", "loadError", "radius"], requireAll = false)
    @JvmStatic
    fun setLoadDrawable(
        imageView: ImageView, loadDrawable: Drawable, loadError: Drawable?, radius: Int = 0
    ) {
//        Glide.with(imageView.getContext()).load(loadDrawable).error(loadError).apply(
//            RequestOptions().transform(
//                CenterCrop(), TopCornersRoundedTransformation(
//                    DisplayUtils.dpToPx(radius)
//                )
//            )
//        ).into(imageView)
        Glide.with(imageView.getContext()).load(loadDrawable).placeholder(loadError).error(loadError).into(imageView)
    }

    @BindingAdapter(value = ["loadUrl", "loadError", "radius"], requireAll = false)
    @JvmStatic
    fun setLoadUrl(
        imageView: ImageView, loadUrl: String?, loadError: Drawable?, radius: Int = 0
    ) {
//        Glide.with(imageView.getContext()).load(loadUrl).error(loadError).apply(
//            RequestOptions().transform(
//                CenterCrop(), TopCornersRoundedTransformation(
//                    DisplayUtils.dpToPx(radius)
//                )
//            )
//        ).into(imageView)
        Glide.with(imageView.getContext()).load(loadUrl).placeholder(loadError).error(loadError).into(imageView)
    }

    @BindingAdapter("android:checked")
    @JvmStatic
    fun setChecked(checkBox: CheckBox, isChecked: Boolean) {
        checkBox.isChecked = isChecked
    }

    @JvmStatic
    @BindingAdapter("isVisible")
    fun goneUnless(view: View, visible: Boolean) {
        view.visibility = if (visible) View.VISIBLE else View.GONE
    }
}